<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="100px">
          <el-form-item label="设备品牌" prop="deviceBrand">
            <el-input v-model="queryParams.deviceBrand" placeholder="请输入设备品牌" clearable style="width: 240px"
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="用户名" prop="account">
            <el-input v-model="queryParams.account" placeholder="请输入用户名或邮箱" clearable style="width: 240px"
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="问题状态" clearable style="width: 240px">
                <el-option v-for="item in status" :key="Number(item.label)" :label="item.value" :value="Number(item.label)"></el-option>
             </el-select>
          </el-form-item>
          <el-form-item label="反馈功能点" prop="feedbackItem">
            <el-input v-model="queryParams.feedbackItem" placeholder="请输入反馈功能点" clearable style="width: 240px"
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="问题发生时间" prop="happenTime">
            <el-date-picker v-model="queryParams.happenTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
              range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="success" plain icon="View" :disabled="single" @click="handleUpdate()"
              v-hasPermi="['system:userFeedback:edit']">查看</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
              v-hasPermi="['system:userFeedback:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport"
              v-hasPermi="['system:userFeedback:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="userFeedbackList" @selection-change="handleSelectionChange" :default-sort="{ prop: 'createTime', order: 'descending' }"
        @sort-change="handleSortChange">
        <el-table-column type="selection" width="55" align="center" />
        <!-- <el-table-column label="邮箱" align="center" prop="email" /> -->
        <el-table-column label="用户名或邮箱" align="center" prop="account" />
        <el-table-column label="反馈类型" align="center" prop="feedType" />
        <el-table-column label="设备品牌" align="center" prop="deviceBrand" />
        <el-table-column label="反馈功能点" align="center" prop="feedbackItem" />
        <el-table-column label="问题发生时间" align="center" prop="happenTime" width="180" :show-overflow-tooltip="true"
          sortable="custom" :sort-orders="['descending', 'ascending']">
          <template #default="scope">
            <span>{{ scope.row.happenTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label='状态' align='center'>
          <template v-slot:header>
            <span>
              状态
              <el-icon><Edit /></el-icon>
            </span>
          </template>
          <template #default="scope">
              <el-tag type="danger" plain size="small" v-if="scope.row.status==0" style="cursor:pointer;"  @click="statusVShow(scope.row)">未处理</el-tag>
              <el-tag type="warning" plain size="small" v-else-if="scope.row.status==1" style="cursor:pointer;"  @click="statusVShow(scope.row)">处理中</el-tag>
              <el-tag type="success"  plain size="small" v-else-if="scope.row.status==2" style="cursor:pointer;"  @click="statusVShow(scope.row)">处理完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="问题创建时间" align="center" prop="createTime" width="180" :show-overflow-tooltip="true"
          sortable="custom" :sort-orders="['descending', 'ascending']">
          <template #default="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button link type="primary" icon="View" @click="handleUpdate(scope.row)"
                v-hasPermi="['system:userFeedback:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                v-hasPermi="['system:userFeedback:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
        @pagination="getList" />
    </el-card>
    <!-- 添加或修改用户反馈记录对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="700px" append-to-body>
      <el-form ref="userFeedbackFormRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="邮箱" prop="email">{{ form.email }}</el-form-item>
        <el-form-item label="设备品牌" prop="deviceBrand">{{ form.deviceBrand }}</el-form-item>
        <el-form-item label="设备型号" prop="deviceModel"> {{ form.deviceModel }}</el-form-item>
        <el-form-item label="错误信息" prop="errMsg">{{ form.errMsg }}</el-form-item>
        <el-form-item label="语言" prop="language">{{ form.language }}</el-form-item>
        <el-form-item label="系统名称" prop="system"> {{ form.system }}</el-form-item>
        <el-form-item label="rom" prop="romName">{{ form.romName }} </el-form-item>
        <el-form-item label="rom版本" prop="romVersion">{{ form.romVersion }}</el-form-item>
        <el-form-item label="用户名或邮箱" prop="account">{{ form.account }}</el-form-item>
        <el-form-item label="反馈功能点" prop="feedbackItem">{{ form.feedbackItem }}</el-form-item>
        <el-form-item label="反馈内容">{{ form.content }}</el-form-item>
        <el-form-item label="反馈图片" prop="picture" v-if="form.picture?.length">
          <el-image v-for="(image, index) in form.picture" :key="index" :src="image"
            style="max-width: 100px; max-height: 100px; margin-right: 10px; margin-bottom: 10px;"></el-image>
        </el-form-item>
        <el-form-item label="问题发生时间" prop="happenTime">{{ parseTime(form.happenTime, '{y}-{m}-{d}') }}</el-form-item>
        <el-form-item label="是否删除" prop="isDelete">{{ form.isDelete ? '否' : '是' }}</el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 修改状态 -->
    <el-dialog title="反馈状态" width='400'  v-model="statusVisible"  append-to-body :close-on-click-modal="false" :close-on-press-escape="false" @close='statusVisible = false'>
            <span style="color:#1A1A1A;font-size:16px;margin-right:20px">反馈结果</span>
            <el-select v-model="statusChange" placeholder="反馈结果" clearable >
                <el-option v-for="item in status" :key="Number(item.label)" :label="item.value" :value="Number(item.label)"></el-option>
            </el-select>
            <template #footer>
              <div class="dialog-footer">
                <el-button :loading="buttonLoading" @click="statusVisible = false" type="primary">取 消</el-button>
                <el-button @click="statusSubmit">确 定</el-button>
              </div>
            </template>
        </el-dialog>
  </div>
</template>

<script setup name="UserFeedback" lang="ts">
import { listUserFeedback, getUserFeedback, delUserFeedback, addUserFeedback, updateUserFeedback,updateUserFeedbackStatus } from '@/api/system/userFeedback';
import { UserFeedbackVO, UserFeedbackQuery, UserFeedbackForm } from '@/api/system/userFeedback/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const userFeedbackList = ref<UserFeedbackVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const userFeedbackFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: UserFeedbackForm = {
  id: undefined,
  email: undefined,
  deviceBrand: undefined,
  deviceModel: undefined,
  errMsg: undefined,
  language: undefined,
  system: undefined,
  romName: undefined,
  romVersion: undefined,
  account: undefined,
  feedType: undefined,
  feedbackItem: undefined,
  content: undefined,
  picture: undefined,
  happenTime: undefined,
  isDelete: undefined,
  status: undefined,
}
const defaultSort = ref<any>({ prop: "happenTime", order: "descending" });
const data = reactive<PageData<UserFeedbackForm, UserFeedbackQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deviceBrand: undefined,
    account: undefined,
    feedType: undefined,
    feedbackItem: undefined,
    status: undefined,
    happenTime: undefined,
    orderByColumn: defaultSort.value.prop,
    isAsc: defaultSort.value.order,
    params: {
    }
  },
  rules: {

  }
});

const { queryParams, form, rules } = toRefs(data);
const statusVisible = ref(false);
const statusChange = ref();
const id = ref(0);
const status = [{value:'未处理',label:0},{value:'处理中',label:1},{value:'处理完成',label:2}];
//显示修改审核状态的弹窗
const statusVShow = (item:any) => {
    statusVisible.value = true;
    statusChange.value = Number(item.status)
    id.value = item.id
}
//修改审核状态
const statusSubmit = async() => {
  buttonLoading.value = true;
  await updateUserFeedbackStatus({id:id.value,status:statusChange.value}).then((res)=>{
    if (res.code == 200) {
        ElMessage.success('修改成功');
        setTimeout(() => {
            getList();
            statusVisible.value = false
        }, 500);
        } else {
          ElMessage.error('修改失败');
        }
  }).finally(() => buttonLoading.value = false);
}
/** 查询用户反馈记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listUserFeedback(queryParams.value);
  userFeedbackList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}
/** 排序触发事件 */
const handleSortChange = (column: any) => {
  queryParams.value.orderByColumn = column.prop;
  queryParams.value.isAsc = column.order;
  getList();
}
/** 取消按钮 */
const cancel = () => {
  dialog.visible = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: UserFeedbackVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
const handleUpdate = async (row?: UserFeedbackVO) => {
  const _id = row?.id || ids.value[0]
  const res = await getUserFeedback(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "用户反馈记录";
}

/** 提交按钮 */
const submitForm = () => {
  userFeedbackFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateUserFeedback(form.value).finally(() => buttonLoading.value = false);
      } else {
        await addUserFeedback(form.value).finally(() => buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: UserFeedbackVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除用户反馈记录编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delUserFeedback(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('system/userFeedback/export', {
    ...queryParams.value
  }, `userFeedback_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
